package com.hxd.mall.base

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.hxd.mall.R
import com.hxd.mall.utils.open.AnimationUtil
import com.trello.rxlifecycle2.LifecycleTransformer
import javax.inject.Inject

/**
 *  fragment基类实现
 */
abstract class BaseInjectFragment<T : IBasePresenter> : DaggerFragment(),
    IBaseView {

    private var alertDialog: Dialog? = null

    internal var mSwipeRefresh: SwipeRefreshLayout? = null
    @Inject
    @JvmField
    protected var mPresenter: T? = null

    private var mContext: Context? = null
    //缓存Fragment view
    var mRootView: View? = null
//    private var mIsMulti = false

    /**
     * 绑定布局文件
     * @return  布局文件ID
     */
    protected abstract fun attachLayoutRes(): Int

    /**
     * 初始化视图控件
     */
    protected abstract fun initViews()

    /**
     * 初始化数据
     */
    protected abstract fun initData()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (mRootView == null) {
            mRootView = inflater.inflate(attachLayoutRes(), container, false)
        }
        return mRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    //默认只处理一次
    private var isDefaultLoaded = false

    //是否显示时都展示
    var isReload = false

    override fun setMenuVisibility(menuVisible: Boolean) {
        Log.d("javaClassName", menuVisible.toString())
        if (menuVisible && mRootView != null && !isDefaultLoaded) {
            initData()

            if (!isReload)
                isDefaultLoaded = true
        } else {
            //do stop load
        }
    }

//    override fun onActivityCreated(savedInstanceState: Bundle?) {
//        super.onActivityCreated(savedInstanceState)
//        mIsMulti = userVisibleHint && mRootView != null && !mIsMulti
//    }
//
//    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
//        mIsMulti = isVisibleToUser && isVisible && mRootView != null && !mIsMulti
//
//        if (!mIsMulti) {
//            //do stop load
//        } else {
//            initData()
//        }
//    }

    override fun <T> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun finishRefresh() {}

    override fun showLoading() {
        if (alertDialog != null && alertDialog!!.isShowing)
            return
        initLoadingDialog()
        alertDialog?.show()
    }

    override fun netSuccess(vararg t: Any) {

    }

    override fun hideLoading() {
        try {
            if (activity == null)
                return
            if (activity?.isFinishing!!)
                return
            if (alertDialog == null)
                return
            if (!alertDialog?.isShowing!!)
                return

            alertDialog?.dismiss()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun isContainFragment() = false

    private fun initLoadingDialog() {
        val dialog = Dialog(context!!, R.style.TranslucentBackground)
        val inflater = LayoutInflater.from(activity).inflate(R.layout.dialog_loading, null).apply {
            findViewById<ImageView>(R.id.iv_dialogloading_img).startAnimation(AnimationUtil.strRotation())
        }
        dialog.setContentView(inflater)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(true)

        alertDialog = dialog
    }

}
